/**
 * Created by liyongliang on 15/9/29.
 */
function Weixin() {
	this.shareData = {
		title: "约泡浪呀榜",
		desc: "一卷温馨浪呀榜  囊尽华夏奇妙汤。",
		link: 'http://m.yidaozou.com/html/langyabang/jump.html',
		// 此处为绝对定位
		imgUrl: "http://m.yidaozou.com/html/langyabang/src/img/slide/share_img@320x320.jpg", // 绝对路径
		type: "",
		dataUrl: "",
		success: function (res) {
		},
		cancel: function (res) {
		}
	};
	var pr = JSON.stringify({
		data: {
			debug: false,
			url: window.location.href
		}
	});
	this.config = $.ajax({
		url: "http://api.one-dao.com/api/wechat/getTicket?parameters=" + encodeURIComponent(pr),
		async: false,
		type: "POST",
		dataType: "jsonp",
		jsonp: "callback",
		jsonpCallback: "jsonpCallback",
		success: function (result) {
			if (result) {
				//result.jsApiList = ["onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", "onMenuShareWeibo"];
				if (wx && wx.error) {
					wx.error(function (res) {
						//alert(JSON.stringify(res));
					});
					wx.config(result);
				}
				window.wx = wx;
			}
		},
		fail: function (result) {
			alert(JSON.stringify(result));
		}
	});

	this.initGeolocation = function () {
		if (wx) {
			wx.getLocation({
				type: 'wgs84', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
				success: function (res) {
					var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
					var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
					alert(latitude);
				}
			});
		}
	};

	this.init = function (shareData) {
		shareData = $.extend(this.shareData, shareData);
		function bindWeixin() {
			function shareAppMessage() {
				WeixinJSBridge.invoke("sendAppMessage", {
					appid: "",
					img_url: shareData.imgUrl,
					img_width: "200",
					img_height: "200",
					link: shareData.link,
					url: shareData.link,
					desc: shareData.desc,
					title: shareData.title
				}, function () {

				})
			}

			function shareTimeline() {
				WeixinJSBridge.invoke("shareTimeline", {
					appid: "",
					img_url: shareData.imgUrl,
					img_width: "200",
					img_height: "200",
					link: shareData.link,
					url: shareData.link,
					desc: shareData.desc,
					title: shareData.title
				}, function () {

				});
			}

			function shareWeibo() {
				WeixinJSBridge.invoke("shareWeibo", {
					appid: "",
					img_url: shareData.imgUrl,
					img_width: "200",
					img_height: "200",
					link: shareData.link,
					url: shareData.link,
					desc: shareData.desc,
					title: shareData.title
				}, function () {

				});
			}

			document.addEventListener("WeixinJSBridgeReady", function () {
				WeixinJSBridge.on("menu:share:appmessage", function () {
					shareAppMessage();
				}), WeixinJSBridge.on("menu:share:timeline", function () {
					shareTimeline();
				}), WeixinJSBridge.on("menu:share:weibo", function () {
					shareWeibo();
				})
			}, false);
		}

		bindWeixin();
		if (wx) {
			wx.ready(function () {
				wx.onMenuShareTimeline({
					title: shareData.title,
					link: shareData.link,
					imgUrl: shareData.imgUrl,
					success: shareData.success || function () {

					},
					cancel: shareData.cancel
				});
				wx.onMenuShareAppMessage({
					title: shareData.title,
					desc: shareData.desc,
					link: shareData.link,
					imgUrl: shareData.imgUrl,
					type: shareData.type,
					dataUrl: shareData.dataUrl,
					success: shareData.success || function () {

					},
					cancel: shareData.cancel
				});
				wx.onMenuShareQQ({
					title: shareData.title,
					desc: shareData.desc,
					link: shareData.link,
					imgUrl: shareData.imgUrl,
					success: shareData.success || function () {

					},
					cancel: shareData.cancel
				});
				wx.onMenuShareWeibo({
					title: shareData.title,
					desc: shareData.desc,
					link: shareData.link,
					imgUrl: shareData.imgUrl,
					success: shareData.success || function () {

					},
					cancel: shareData.cancel
				});
			});

		}
	};
	window.WeiXin = this;
}